package com.xzy.demo.server;

import com.xzy.demo.dao.StaffRepository;
import com.xzy.demo.entry.Staff;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

@Service
public class StaffServiceImpl implements StaffService {

    @Autowired private StaffRepository dao;


    @Override
    public void delStaff(Staff staff) {
        dao.delete(staff);
    }

    @Override
    public void saveStaff(Staff staff) {
        dao.save(staff);
    }

    @Override
    public int updateStaff(Staff staff) {
        dao.save(staff);
        return 1;
    }


    @Override
    public Staff getStaffById(Integer id) {
        return dao.getOne(id);
    }

    @Override
    public Staff getStaffByName(String name){
        return dao.getStaffByName(name);
    }

    @Override
    public Page<Staff> findStaffList(int page, int rows) {
        Sort sort = Sort.by(Sort.Direction.ASC, "id");
        PageRequest pageRequest = PageRequest.of(page-1, rows, sort);
        Page<Staff> all = dao.findAll(pageRequest);
        return all;

    }
}
